# Contributor: Simon Frankenberger <simon-alpine@fraho.eu>
# Maintainer: Simon Frankenberger <simon-alpine@fraho.eu>
pkgname=openjfx11
pkgver=11.0.4_p1
_pkgver=${pkgver/_p/+}
pkgrel=1
pkgdesc="Open JavaFX 11"
url="https://hg.openjdk.java.net/openjfx/11-dev/rt"
arch="" # fails on aarch64&ppc64le and hangs then
# openjdk11 is not yet available on 32 bit arches
license="GPL-2.0-with-classpath-exception"
depends="$pkgname-gtk3" # for the virtual openjfx11 package
makedepends="binutils-gold
	cmake
	ninja
	python2
	gperf
	openjdk11-jdk
	alsa-lib-dev
	ffmpeg-dev
	freetype-dev
	gtk+2.0-dev
	gtk+3.0-dev
	icu-dev
	ruby-dev
	sqlite-dev"
subpackages="$pkgname-media:_media
	$pkgname-web:_web
	$pkgname-jmods:_jmods:noarch
	$pkgname-src:_src:noarch
	$pkgname-doc:_doc:noarch
	$pkgname-gtk2:_gtk2
	$pkgname-gtk3:_gtk3
	$pkgname-libs:_libs
	$pkgname-base:_base:noarch
	$pkgname-scenebuilder:_scenebuilder:noarch
	$pkgname-dev:_dev:noarch"
source="openjfx-$_pkgver.tar.bz2::http://hg.openjdk.java.net/openjfx/11-dev/rt/archive/$_pkgver.tar.bz2
	https://archive.apache.org/dist/ant/binaries/apache-ant-1.8.2-bin.zip

	configure-ant-home.patch
	crosscompile.patch
	disable-architecture-verification.patch
	disable-assembler-on-unsupported-archs.patch
	disable-jit-for-non-x86.patch
	disable-sse2.patch
	disable-webkit-sampling-profiler.patch
	fix-arm32-build.patch
	fix-arm64-build.patch
	gcc-no-error.patch
	use-system-icu.patch
	webcore-linker-flags.patch
	webkit-debug-level.patch

	scenebuilder.bin"
builddir="$srcdir/rt-$_pkgver"

_ant_home="$srcdir/apache-ant-1.8.2/"
_java_home="/usr/lib/java/openjfx11"

sonameprefix="$pkgname:"
ldpath="$_java_home/lib"

prepare() {
	default_prepare

	# Create build properties
	cat >gradle.properties <<EOF
PROMOTED_BUILD_NUMBER=${_pkgver##*+}
COMPANY_NAME=Alpine
HUDSON_JOB_NAME=abuild
RELEASE_SUFFIX=-alpine
COMPILE_WEBKIT=true
COMPILE_MEDIA=true
# non standard, alpine patched properties
ANT_HOME=$_ant_home
EXTRA_CXXFLAGS=$CXXFLAGS
EXTRA_LDFLAGS=$LDFLAGS
PKG_CONFIG_PATH=$PKG_CONFIG_PATH
PKG_CONFIG_SYSROOT_DIR=$PKG_CONFIG_SYSROOT_DIR
CC=${CC:-gcc}
CXX=${CXX:-g++}
AR=${AR:-ar}
EOF

	# use system provided library for icu
	rm -rf modules/javafx.web/src/main/native/Source/ThirdParty/icu

	# make gradle wrapper executable
	chmod +x ./gradlew
}

build() {
	./gradlew publicExportsStandaloneLinux
}

check() {
	./gradlew check -x :web:test
}

package() {
	mkdir -p "$pkgdir/$_java_home/apps"
	cp -r build/artifacts/javafx-sdk-11*/*                         "$pkgdir/$_java_home"
	cp -r build/artifacts/javafx-jmods-11*/                        "$pkgdir/$_java_home/jmods"
	cp -r build/artifacts/javafx-docs-11*/                         "$pkgdir/$_java_home/api"
	cp apps/scenebuilder/SceneBuilderApp/dist/SceneBuilderApp.jar  "$pkgdir/$_java_home/apps"
	cp apps/scenebuilder/SceneBuilderKit/dist/SceneBuilderKit.jar  "$pkgdir/$_java_home/apps"
}

_jmods() {
	pkgdesc="$pkgdesc (jmods)"
	depends=""
	_fromroot="$pkgdir/$_java_home"
	_toroot="$subpkgdir/$_java_home"

	mkdir -p "$_toroot"
	mv "$_fromroot/jmods" "$_toroot"
}

_src() {
	pkgdesc="$pkgdesc (src)"
	depends=""
	_fromroot="$pkgdir/$_java_home"
	_toroot="$subpkgdir/$_java_home"

	mkdir -p "$_toroot/lib"
	mv "$_fromroot/lib/src.zip" "$_toroot/lib/src.zip"
}

_doc() {
	pkgdesc="$pkgdesc (Documentation)"
	depends=""
	_fromroot="$pkgdir/$_java_home"
	_toroot="$subpkgdir/$_java_home"

	mkdir -p "$_toroot"
	mv "$_fromroot/api" "$_toroot"
}

_gtk2() {
	pkgdesc="$pkgdesc (GTK 2)"
	depends="$pkgname-base"
	_fromroot="$pkgdir/$_java_home"
	_toroot="$subpkgdir/$_java_home"

	mkdir -p "$_toroot/lib"
	mv "$_fromroot/lib/libglassgtk2.so"   "$_toroot/lib"
}

_gtk3() {
	pkgdesc="$pkgdesc (GTK 3)"
	depends="$pkgname-base"
	_fromroot="$pkgdir/$_java_home"
	_toroot="$subpkgdir/$_java_home"

	mkdir -p "$_toroot/lib"
	mv "$_fromroot/lib/libglassgtk3.so"   "$_toroot/lib"
}

_scenebuilder() {
	pkgdesc="$pkgdesc (SceneBuilder)"
	depends="$pkgname-gtk3 $pkgname-web dbus"
	_fromroot="$pkgdir/$_java_home"
	_toroot="$subpkgdir/$_java_home"

	mkdir -p "$_toroot/lib"
	mv \
		"$_fromroot/apps/SceneBuilderApp.jar" \
		"$_fromroot/apps/SceneBuilderKit.jar" \
		"$_toroot/lib"

	mkdir -p "$_toroot/bin"
	install -m 0755 "$srcdir/scenebuilder.bin" "$_toroot/bin/scenebuilder"

	mkdir -p "$subpkgdir/usr/bin"
	ln -s "$_java_home/bin/scenebuilder" "$subpkgdir/usr/bin/scenebuilder11"
}

_web() {
	pkgdesc="$pkgdesc (web)"
	depends="$pkgname-media"
	_fromroot="$pkgdir/$_java_home"
	_toroot="$subpkgdir/$_java_home"

	mkdir -p "$_toroot/lib"
	mkdir -p "$_toroot/jmods"
	mkdir -p "$_toroot/legal"
	mv \
		"$_fromroot"/lib/libjfxwebkit.so \
		"$_fromroot"/lib/javafx.web.jar \
		"$_toroot/lib"
	mv \
		"$_fromroot"/jmods/javafx.web.jmod \
		"$_toroot/jmods"
	mv \
		"$_fromroot/legal/javafx.web" \
		"$_toroot/legal"
}

_media() {
	pkgdesc="$pkgdesc (media)"
	depends="$pkgname-libs"
	_fromroot="$pkgdir/$_java_home"
	_toroot="$subpkgdir/$_java_home"

	mkdir -p "$_toroot/lib"
	mkdir -p "$_toroot/jmods"
	mkdir -p "$_toroot/legal"

	mv \
		"$_fromroot"/lib/libavplugin.so \
		"$_fromroot"/lib/libgstreamer-lite.so \
		"$_fromroot"/lib/libjfxmedia.so \
		"$_fromroot"/lib/libfxplugins.so \
		"$_fromroot"/lib/javafx.media.jar \
		"$_toroot/lib"
	mv \
		"$_fromroot"/jmods/javafx.media.jmod \
		"$_toroot/jmods"
	mv \
		"$_fromroot/legal/javafx.media" \
		"$_toroot/legal/"
}

_libs() {
	pkgdesc="$pkgdesc (libs)"
	depends=""
	_fromroot="$pkgdir/$_java_home"
	_toroot="$subpkgdir/$_java_home"

	mkdir -p "$_toroot/lib"
	mv "$_fromroot"/lib/*.so   "$_toroot/lib"
}

_base() {
	pkgdesc="$pkgdesc (base)"
	depends="$pkgname-libs openjdk11-jre"
	_fromroot="$pkgdir/$_java_home"
	_toroot="$subpkgdir/$_java_home"

	mkdir -p "$_toroot"
	mv "$_fromroot/legal" "$_toroot"
	mv "$_fromroot/lib"   "$_toroot"
}

_dev() {
	pkgdesc="$pkgdesc (development)"
	depends="$pkgname-jmods $pkgname-src $pkgname-doc $pkgname-media $pkgname-web $pkgname-scenebuilder"
	mkdir -p "$subpkgdir"
}

sha512sums="10068828bf3f63782d734ccad3f385a9b092905fcd71caa6052fcbceb8108706f85c6e47ddb43aceea2dc84a7b8d8ef6ea91d820faa484b03c2cfff61b230bc1  openjfx-11.0.4+1.tar.bz2
b10a420f65dc47b609fd3b7b0fae5b0bd1ddfa6a52cad41bc729c03f7ff82a79092ec81a24cdae363b8b6ec05061267e3cc48a0e14e097d07757bc021cb5619b  apache-ant-1.8.2-bin.zip
076efe734b42bf2ff5979693590710cbebff58bcd789645e3cf7646381fe18b3b9057d20e3491785f6ef690a06094ab3fe7dea5ae8c9eb3fe1e38940e4ece181  configure-ant-home.patch
7356cb3139f8dd779e2531d16008bb08ccdac2add3f60f956a592a1de32005d2becc95895d8c9e49bd6ac94f13f93add1e9652987f07fc91aa15ed761f34f0ec  crosscompile.patch
33a3e4067347a51f5a8dacd3827e6108aefce1dd6a8b39847b1af4652834ffc36d69622023f18cd6cad73c7a3e2e65b51cf14e57f96bf4152385838f82f99646  disable-architecture-verification.patch
c508a813d8c21151396fd385b6807f757236d0ddb0e33db21368c61c554f845c01326c774eb515a52b098ec51c55f77d1b9c185fba8f6dd0bd4aaf2b32e09880  disable-assembler-on-unsupported-archs.patch
71226d10a94bf08eefc64b58d577490a1f98316574daf3142e33c2eec0ea714487cbf6e6eb6eda13c76bd56186cf5d7f894482470c3abdfc2849841149924b1b  disable-jit-for-non-x86.patch
f9d8396d94cd8776ed81298d6139ccdfc98fa2fb99591b2998a6f79038be5656f2a7f8eb8610ee60dc4327476147400e8184eafef5598dabeaf5eed97adc290c  disable-sse2.patch
3d443a7216b5f0df3eca1ceb7da9cb973f17d015707cad0203972dee8af1422c3204bd77af36571ded0827a56a21f2f355bb657c1e2ff42da2780667bc8cb995  disable-webkit-sampling-profiler.patch
fd29cad92fa81f65d018b8319432fa665960ac2dba43433e186a001ece488447b34bcc63f8a8d75e0bfebef63a4745d2a54b01024b1423ba17d8445b25619893  fix-arm32-build.patch
3f7ec35437b94e8773541b8cfbf99c2225540572b0c2ba50a954824aba9ad374085fb1b3532ff7bb53a02346b9b5ef7804acf980016815c56acdf587f6559613  fix-arm64-build.patch
9a1c3270786adcf2d7d3f21ff1fc08fbff86ef8175f39eee9b58a7ae4f7923565b25ed5a11721669be89518b1568ab45f7e3b046a91884a2962b37178caa55a5  gcc-no-error.patch
1ad39f3d53ab52696f8ec6c1039afa494949de1c261dd3c0f9f68372860da2a9c601b09c92b61c9099b399c7589f7f2e313bf93aabaabde0e294ddb7eec60b92  use-system-icu.patch
2b8ec3bae8c93e5b6154988a5a7ac53477b7e86eea00d11cbd5ac1aaa5c87ddeaba239a65423549da29647f839c15d0f25126128f8fa619ea11ae907be646b44  webcore-linker-flags.patch
232aebbd13e2e3291d1ae2bc0df282ade5e861e17fce9aaf378ea930655137a2f3fd6e8b43b1ffbc9c3fcc5b21ec36129bdff1070253d6642162e099b32fc171  webkit-debug-level.patch
3e37adab75b12c4628cb8b43ef5ae83efdf29d1b4721ff5e1c8f89ea44b53837e4a596be7139a24a3d6d631dcc44ba0050212fba5be8bf19d5a3db6441610d90  scenebuilder.bin"
